package com.example.demo.controller;

import com.example.demo.entity.Bill;
import com.example.demo.service.BillService;
import com.example.demo.util.ToolUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class BillListController {

    @Autowired
    BillService billService;

    @GetMapping("/billList")
    public String billList(Model model) {
        List<Bill> billByWhere = billService.findBillsByWhere(null);
        model.addAttribute("billList", billByWhere);
        return "page/billList";
    }


    @GetMapping("/searchByWhere")
    public String searchByWhere(Bill bill, Model model) {
        List<Bill> billByWhere = billService.findBillsByWhere(bill);
        model.addAttribute("billList", billByWhere);
        model.addAttribute("searchWhere", bill);
        return "page/billList";
    }


    @GetMapping("/toBillAddPage")
    public String toBillAddPage() {
        return "page/billAdd";
    }


    @PostMapping("/toAddBill")
    public String toAddBill(Bill bill, Model model) {

        bill.setCreateTime(new Date());
        int i = billService.insertBill(bill);
        if (i > 0) {
            return "redirect:/billList";//请求之间的调用
        } else {
            model.addAttribute("adderror", "添加失败请检查数据");
            return "page/billAdd";
        }

    }


    @GetMapping("/toBillViewPage")
    public String toBillViewPage(Integer id, Model model) {

        Bill bill = billService.detailBill(id);
        model.addAttribute("bill", bill);
        return "page/billView";
    }


    @GetMapping("/toBillUpdatePage")
    public String toBillUpdatePage(Integer id, Model model) {
        Bill bill = billService.detailBill(id);
        model.addAttribute("bill", bill);
        return "page/billUpdate";
    }


    @PostMapping("/toUpdate")
    public String toUpdate(Bill bill, Model model) {
        String createTimeStr = bill.getCreateTimestr();
        //  Date date = ToolUtils.StringParseDate(createTimeStr);
        // bill.setCreateTime(new Date());
        try {
            int i = billService.updateBill(bill);
            if (i > 0)
                return "redirect:/billList";//请求之间的调用
            else
                return "page/billUpdate";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "更新失败服务器内部错误！" +
                    "请联系管理员");
            return "page/billUpdate";
        }
    }


    @GetMapping("/deleteBillById")
    public String deleteBillById(Integer id) {

        int result = billService.deleteBillById(id);
        if (result > 0) {

        } else {

        }


        return "redirect:/billList";//请求之间的调用
    }


}
